package com.xiaoyu.izf.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.xiaoyu.izf.model.*;
import com.xiaoyu.izf.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;

@Service
public class OrderInfoService {
    @Autowired
    private GoodsOrderDao goodsOrderDao;
    @Autowired
    private VipOrderDao vipOrderDao;
    @Autowired
    private OrderInfoDao orderInfoDao;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private CommissionLogDao commissionLogDao;
    @Autowired
    private GoodsOrderService goodsOrderService;
    @Autowired
    private UserInfoService userInfoService;

    public OrderInfo orderInfo(String outTradeNo){
        return orderInfoDao.findOneByOutTradeNo(outTradeNo);
    }

    public OrderInfo vipOrderAdd(String outTradeNo, VipOrder vipOrder, String clientIp,int totAmount,int isTest){
        OrderInfo order = new OrderInfo();
        order.setOutTradeNo(outTradeNo);
        order.setUid(vipOrder.getUid());
        order.setClientIp(clientIp);
        order.setOrderStatus(0);
        order.setChannel("alipay");
        order.setOrderType(1);
        order.setOrderId(vipOrder.getId());
        order.setTotAmount(totAmount);
        order.setInsertTime(new Timestamp(System.currentTimeMillis()));
        order.setPayTime(new Timestamp(System.currentTimeMillis()));
        order.setIsTest(isTest);
        vipOrder.setOutTradeNo(outTradeNo);
        vipOrderDao.save(vipOrder);
        return orderInfoDao.save(order);
    }

    public OrderInfo goodsOrderAdd(String outTradeNo, GoodsOrder goodsOrder, String clientIp,int totAmount,int isTest){
        OrderInfo order = new OrderInfo();
        order.setOutTradeNo(outTradeNo);
        order.setUid(goodsOrder.getUid());
        order.setClientIp(clientIp);
        order.setOrderStatus(0);
        order.setChannel("alipay");
        order.setOrderType(2);
        order.setOrderId(goodsOrder.getId());
        order.setTotAmount(totAmount);
        order.setInsertTime(new Timestamp(System.currentTimeMillis()));
        order.setPayTime(new Timestamp(System.currentTimeMillis()));
        order.setIsTest(isTest);
        goodsOrder.setOutTradeNo(outTradeNo);
        goodsOrder.setChannel("alipay");
        goodsOrderDao.save(goodsOrder);
        return orderInfoDao.save(order);
    }

    public void orderPay(String outTradeNo){
        OrderInfo order = orderInfoDao.getOne(outTradeNo);
        order.setOrderStatus(1);
        order.setPayTime(new Timestamp(System.currentTimeMillis()));
        orderInfoDao.save(order); //订单已支付
        UserInfo userInfo = userInfoDao.getOne(order.getUid());
        if(order.getOrderType()==1){//vip订单
            VipOrder vipOrder = vipOrderDao.getOne(order.getOrderId());
            vipOrder.setOrderStatus(1);
            vipOrder.setPayTime(new Timestamp(System.currentTimeMillis()));
            if(vipOrder.getPushUid()>0&&userInfo.getIsTest()==0){
                UserInfo user = userInfoDao.getOne(vipOrder.getPushUid());
                if(user.getVipType()>0) {
                    user.setCommission(user.getCommission() + vipOrder.getPushAmount());
                    user.setCommissionDeposit(user.getCommissionDeposit() + vipOrder.getPushAmount());
                    userInfoDao.save(user);
                    CommissionLog log = commissionLogDao.findOneByOrderIdAndGradeType(vipOrder.getId(), 1);
                    if (log != null) {
                        log.setPayStatus(1);
                        commissionLogDao.save(log);
                    }
                }
            }
            if(vipOrder.getGradePushUid()>0&&userInfo.getIsTest()==0){
                UserInfo user = userInfoDao.getOne(vipOrder.getGradePushUid());
                if(user.getVipType()>0) {
                    user.setCommission(user.getCommission() + vipOrder.getGradeAmount());
                    user.setCommissionDeposit(user.getCommissionDeposit() + vipOrder.getGradeAmount());
                    userInfoDao.save(user);
                    CommissionLog log = commissionLogDao.findOneByOrderIdAndGradeType(vipOrder.getId(), 2);
                    if (log != null) {
                        log.setPayStatus(1);
                        commissionLogDao.save(log);
                    }
                }
            }
            vipOrderDao.save(vipOrder);

            userInfo.setToTheAccount(1);
            userInfo.setVipType(vipOrder.getVipType());
/*            if(vipOrder.getVipType()==1){
                userInfo.setOfficialServiceNumber("CAZFA"+userInfo.getOfficialServiceNumber().substring(5));
            }else if(vipOrder.getVipType()==2){
                userInfo.setOfficialServiceNumber("CAZFB"+userInfo.getOfficialServiceNumber().substring(5));
            }else if(vipOrder.getVipType()==3){
                userInfo.setOfficialServiceNumber("CAZFC"+userInfo.getOfficialServiceNumber().substring(5));
            }*/
            if(StringUtils.isEmpty(userInfo.getOfficialServiceNumber())||"粉丝".equals(userInfo.getOfficialServiceNumber())){
                int n = userInfoService.regUserCount(DateUtil.format(userInfo.getRegTime(),"yyyy-MM"))+1;
                while (true) {
                    String osn = DateUtil.format(DateUtil.date(), "yyyyMM") + NumberUtil.decimalFormat("000", n);
                    UserInfo userInfo1 = userInfoDao.findByOfficialServiceNumber(osn);
                    if(userInfo1==null) {
                        userInfo.setOfficialServiceNumber(osn);
                        break;
                    }
                    n++;
                }
            }
            userInfoDao.save(userInfo);
        }
        if(order.getOrderType()==2){//商品支付
            GoodsOrder goodsOrder = goodsOrderDao.getOne(order.getOrderId());
            goodsOrder.setOrderStatus(1);
            goodsOrder.setPayTime(new Timestamp(System.currentTimeMillis()));
            goodsOrderDao.save(goodsOrder);
            goodsOrderService.goodsSaleNumAdd(goodsOrder.getGoodsId(),goodsOrder.getBuyNum());
        }
    }
}
